<template>
	<el-dialog v-model="visible" :title="title" :close-on-click-modal="false" :width="900">
		<div style="font-weight: 600; line-height: 30px; background: #f7f8fa; color: #1d2129; padding-left: 16px; margin-bottom: 10px">患者基本信息</div>

		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" class="patient-form">
			<el-row>
				<el-col :span="8">
					<el-form-item prop="pttNm" label="姓名">
						<el-input v-model="dataForm.pttNm" style="width: 100%" placeholder="请输入姓名"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="gdrCd" label="性别" style="width: 100%">
						<fast-select
							disabled
							v-model="dataForm.gdrCd"
							style="width: 100%"
							dict-type="mpi_gdr_cd"
							placeholder="请选择性别"
							clearable
							filterable
						></fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="age" label="年龄">
						<el-input style="width: 100%" v-model="dataForm.age" disabled placeholder="请输入年龄"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="8">
					<el-form-item prop="idCard" label="证件类型" style="width: 100%">
						<el-select disabled v-model="dataForm.idCard" style="width: 100%">
							<el-option label="身份证" value="01">身份证</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="idCrdNo" label="证件号">
						<el-input style="width: 100%" v-model="dataForm.idCrdNo" placeholder="请输入证件号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="slfTelNo" label="联系电话">
						<el-input v-model="dataForm.slfTelNo" style="width: 100%" placeholder="请输入联系电话"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="aboCd" label="ABO血型">
						<fast-select
							v-model="dataForm.aboCd"
							style="width: 100%"
							dict-type="mpi_abo_cd"
							placeholder="请选择ABO血型"
							clearable
							filterable
						></fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="rhCd" label="Rh血型">
						<fast-select
							v-model="dataForm.rhCd"
							style="width: 100%"
							dict-type="mpi_rh_cd"
							placeholder="请选择Rh血型"
							clearable
							filterable
						></fast-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="ntnlyCd" label="国籍">
						<fast-select v-model="dataForm.ntnlyCd" style="width: 100%" dict-type="mpi_ntnly_cd" placeholder="请选择国籍" clearable filterable>
						</fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="ntnCd" label="民族">
						<fast-select
							v-model="dataForm.ntnCd"
							style="width: 100%"
							dict-type="mpi_ntn_cd"
							placeholder="请选择民族"
							clearable
							filterable
						></fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="bthDt" label="出生日期">
						<el-input disabled v-model="dataForm.bthDt" style="width: 100%" placeholder="请选择出生日期"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="mryCdtCd" label="婚姻状况">
						<fast-select v-model="dataForm.mryCdtCd" style="width: 100%" placeholder="请选择婚姻状况" dict-type="mpi_mry_cdt_cd" clearable filterable>
						</fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="eduCd" label="学历">
						<fast-select
							v-model="dataForm.eduCd"
							style="width: 100%"
							placeholder="请选择学历"
							dict-type="mpi_edu_cd"
							clearable
							filterable
						></fast-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="wrkUntNm" label="单位名称">
						<el-input v-model="dataForm.wrkUntNm" style="width: 100%" placeholder="请输入工作单位名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="wrkUntTelNo" label="单位电话">
						<el-input v-model="dataForm.wrkUntTelNo" style="width: 100%" placeholder="请输入工作单位电话"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="mdcrCgyCd" label="医疗支付方式">
						<fast-select
							v-model="dataForm.mdcrCgyCd"
							style="width: 100%"
							placeholder="请选择医疗支付方式"
							dict-type="mpi_mdcr_cgy_cd"
							clearable
							filterable
						>
						</fast-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="pfsCgyCd" label="职业类别">
						<fast-select v-model="dataForm.pfsCgyCd" style="width: 100%" placeholder="请选择职业类别" dict-type="mpi_pfs_cgy_cd" clearable filterable>
						</fast-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item prop="dthFlg" label="本人死亡标识">
						<el-radio-group v-model="dataForm.dthFlg" style="width: 100%">
							<el-radio label="0">未死亡</el-radio>
							<el-radio label="1">已死亡</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="8"></el-col>
				<el-col :span="8"></el-col>
			</el-row>

			<!-- <el-form-item prop="joinWrkDt" label="参加工作日期">
				<el-date-picker v-model="dataForm.joinWrkDt" style="width: 100%" type="date" placeholder="请选择工作日期" />
			</el-form-item>
			<el-form-item prop="dthFlg" label="本人死亡标识">
				<el-select filterable v-model="dataForm.dthFlg" style="width: 100%">
					<el-option :label="'已死亡'" :value="'1'"></el-option>
					<el-option :label="'未死亡'" :value="'0'"></el-option>
				</el-select>
			</el-form-item> -->
		</el-form>

		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="btnLoading" @click="saveFormBtn">保存</el-button>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import timeFormater from 'time-formater'
import { savePatient, isExistIdCard } from '@/api/patient-primary/patient-mana'
import { ElMessage } from 'element-plus'
const visible = ref(false)
const title = ref('')
const emit = defineEmits(['close'])

const btnLoading = ref(false)

const dataFormRef = ref()
const dataForm = reactive({
	idCard: '01',
	currAddrDscpt: '',
	pttNm: '',
	gdrCd: '',
	idCrdNo: '',
	slfTelNo: '',
	aboCd: '',
	rhCd: '',
	ntnlyCd: '',
	ntnCd: '',
	bthDt: '',
	mryCdtCd: '',
	eduCd: '',
	wrkUntNm: '',
	wrkUntTelNo: '',
	mdcrCgyCd: '',
	pfsCgyCd: '',
	joinWrkDt: '',
	age: '',
	dthFlg: '0'
})

const dataRules = ref({
	pttNm: [
		{
			required: true,
			message: '姓名为必填项',
			trigger: 'blur'
		}
	],
	idCard: [
		{
			required: true,
			message: '证件类型为必填项',
			trigger: 'blur'
		}
	],
	idCrdNo: [
		{
			required: true,
			validator: (rule, value, callback) => {
				if (value == '') {
					dataForm.age = ''
					dataForm.gdrCd = ''
					dataForm.bthDt = ''
					return callback(new Error('证件号为必填项'))
				}

				let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
				// let _IDre15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
				// 校验身份证：
				if (_IDRe18.test(value) && value.length == 18) {
					dataForm.age = idCardToAge(value).age
					dataForm.gdrCd = idCardToAge(value).sex
					dataForm.bthDt = idCardToAge(value).birthday
					isExistIdCardCheck(value, callback)
					callback()
				} else {
					return callback(new Error('请输入18位正确的证件号'))
				}
			}
		}
	],
	slfTelNo: [
		{
			required: true,
			trigger: 'blur',
			validator: (rule, value, callback) => {
				if (value == '') {
					return callback(new Error('联系电话为必填项'))
				}
				let _tel = /^1[3456789]\d{9}$|^0\d{2,3}-?\d{7,8}$/
				if (_tel.test(value)) {
					callback()
				}
				return callback(new Error('请输入正确的联系电话'))
			}
		}
	]
})

const isExistIdCardCheck = (value, callback) => {
	isExistIdCard({ idcrd: value })
		.then(res => {})
		.catch(() => {})
}

const idCardToAge = userCard => {
	let yearBirth: any = ''
	let monthBirth: any = ''
	let dayBirth: any = ''
	let sex: any = ''
	if (userCard.length == 18) {
		//获取出生年月日 性别
		yearBirth = userCard.substring(6, 10)
		monthBirth = userCard.substring(10, 12)
		dayBirth = userCard.substring(12, 14)
		sex = userCard.substring(16, 17) % 2 == 0 ? '2' : '1'
	}
	//获取当前年月日并计算年龄
	var myDate = new Date()
	var monthNow = myDate.getMonth() + 1
	var dayNow = myDate.getDate()
	var age = myDate.getFullYear() - yearBirth
	if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
		age--
	}
	//返回年龄、出生年月
	return { age: age, birthday: yearBirth + '-' + monthBirth + '-' + dayBirth, sex }
}
const init = () => {
	visible.value = true
	title.value = '新建患者主索引'
	btnLoading.value = false

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
}

const saveFormBtn = () => {
	dataFormRef.value.validate(valid => {
		if (!valid) return
		// 表单验证通过，进行表单提交

		if (dataForm.joinWrkDt) {
			dataForm.joinWrkDt = timeFormater(dataForm.joinWrkDt).format('YYYY-MM-DD')
		}

		savePatient(dataForm)
			.then(res => {
				ElMessage.success({
					message: '保存成功',
					duration: 500,
					onClose: () => {
						btnLoading.value = false
						visible.value = false
						emit('close')
					}
				})
			})
			.catch(() => {
				btnLoading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
<style lang="scss" scoped>
.patient-form .el-form--inline .el-form-item {
	width: 100%;
}
</style>
